//
//  htvregister.m
//  MaiYaApp
//
//  Created by zhangwq on 15/11/20.
//  Copyright © 2015年 HT. All rights reserved.
//

#import "htvregister.h"
#import "HTFrameKit.h"
#import "HTSysKit.h"
#import "HTFrameConvert.h"
#import "htvloadcity.h"

@interface htvregister (){
    CGFloat width;
    CGFloat height;
    UITextField *txttel;
    UITextField *txtno;
    UITextField *txtpassword;
    UITextField *txtpassword2;
    UITextField *txtname;
    UITextField *txtcompanyname;
    UIButton *btnSelectCity;
    NSString *cityCode;
    NSString *cityName;
    UIButton *btnsend;
    NSTimer *timer;
    UIWebView *webview;
    int score;
    UIColor *backcolor;
}

@end

@implementation htvregister

- (void)viewDidLoad {
    [super viewDidLoad];
    
    CGRect rect = [[UIScreen mainScreen] bounds];
    CGSize size = rect.size;
    width = size.width;
    height = size.height;
    
    txttel=[[UITextField alloc]init];
    txtno=[[UITextField alloc]init];
    txtpassword=[[UITextField alloc]init];
    txtpassword2=[[UITextField alloc]init];
    btnSelectCity=[[UIButton alloc]init];
    btnsend=[[UIButton alloc]init];
    timer=[[NSTimer alloc]init];
    txtname=[[UITextField alloc]init];
    txtcompanyname=[[UITextField alloc]init];
    
    cityCode=@"";
    cityName=@"";
    score=120;
    
    backcolor=[UIColor colorWithRed:150.0/255 green:150.0/255 blue:150.0/255 alpha:1.0f];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(moveKeyAction:)];
    [self.view addGestureRecognizer:tapGesture];
    
    [self.navigationController.navigationBar setTitleTextAttributes:
     
     @{NSFontAttributeName:[UIFont systemFontOfSize:16],
       
       NSForegroundColorAttributeName:[UIColor blackColor]}];
    
    [self createControl];

}

-(void)loading{
    [txttel resignFirstResponder];
    [txtno resignFirstResponder];
    [txtpassword resignFirstResponder];
    [txtpassword2 resignFirstResponder];
    webview=[[UIWebView alloc]initWithFrame:CGRectMake(-10, 0, width+10, height)];
    webview.backgroundColor=[UIColor colorWithRed:0.0/255 green:0.0/255 blue:0.0/255 alpha:0.3f];
    webview.opaque=NO;
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *resPath = [bundle resourcePath];
    NSString *filePath = [resPath stringByAppendingPathComponent:@"loading.html"];
    
    [webview loadHTMLString:[NSString stringWithContentsOfFile:filePath]
                    baseURL:[NSURL fileURLWithPath:[bundle bundlePath]]];
    [self.view addSubview:webview];
}

-(void)createControl{
    
   
    UIView *uv1=[[UIView alloc]initWithFrame:CGRectMake(0, 90, width, 40)];
    uv1.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:uv1];
    
    UIView *uv2=[[UIView alloc]initWithFrame:CGRectMake(0, 135, width, 40)];
    uv2.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:uv2];
    
    
    UIView *uv3=[[UIView alloc]initWithFrame:CGRectMake(0, 180, width, 40)];
    uv3.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:uv3];
    
    UIView *uv4=[[UIView alloc]initWithFrame:CGRectMake(0, 225, width, 40)];
    uv4.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:uv4];
    
    UIView *uv5=[[UIView alloc]initWithFrame:CGRectMake(0, 270, width, 40)];
    uv5.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:uv5];
    
    UIView *uv6=[[UIView alloc]initWithFrame:CGRectMake(0, 315, width, 40)];
    uv6.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:uv6];
    
    
    
    
    //手机号
    
    
    txttel=[[UITextField alloc]initWithFrame:CGRectMake(60, 5, width-120, 30)];
    txttel.textColor=[UIColor blackColor];
    txttel.tintColor=backcolor;
    txttel.backgroundColor=[UIColor clearColor];
    txttel.placeholder=@"手机号";
    txttel.font=[UIFont fontWithName:@"Arial" size:14.0];
    [uv1 addSubview:txttel];
    
    //验证码
    
    
    txtno=[[UITextField alloc]initWithFrame:CGRectMake(60, 5, 100, 30)];
    txtno.textColor=[UIColor blackColor];
    txtno.tintColor=backcolor;
    txtno.backgroundColor=[UIColor clearColor];
    txtno.placeholder=@"验证码";
    txtno.font=[UIFont fontWithName:@"Arial" size:14.0];
    [uv2 addSubview:txtno];
    
    
    btnsend=[[UIButton alloc]initWithFrame:CGRectMake(width-70, 5, 60, 30)
             ];
    [btnsend setTitle:@"发送" forState:UIControlStateNormal];
    [btnsend setTitleColor:backcolor forState:UIControlStateNormal];
    btnsend.font=[UIFont fontWithName:@"Arial" size:11.0];
    [btnsend addTarget:self action:@selector(sendAction:) forControlEvents:UIControlEventTouchUpInside];
    btnsend.layer.borderWidth=0.6;
    btnsend.layer.borderColor=backcolor.CGColor;
    btnsend.layer.cornerRadius=4;
    [uv2 addSubview:btnsend];
    
    
    //密码

    
    txtpassword=[[UITextField alloc]initWithFrame:CGRectMake(60, 5, width-120, 30)];
    txtpassword.textColor=[UIColor blackColor];
    txtpassword.tintColor=backcolor;
    txtpassword.backgroundColor=[UIColor clearColor];
    txtpassword.placeholder=@"密码";
    txtpassword.font=[UIFont fontWithName:@"Arial" size:14.0];
    txtpassword.secureTextEntry = YES;

    [uv3 addSubview:txtpassword];
    
    
    //queren密码

    txtpassword2=[[UITextField alloc]initWithFrame:CGRectMake(60, 5, width-120, 30)];
    txtpassword2.textColor=[UIColor blackColor];
    txtpassword2.tintColor=backcolor;
    txtpassword2.backgroundColor=[UIColor clearColor];
    txtpassword2.placeholder=@"确认密码";
    txtpassword2.font=[UIFont fontWithName:@"Arial" size:14.0];
    txtpassword2.secureTextEntry = YES;
    [uv4 addSubview:txtpassword2];
    
    
    //用户名
    txtname=[[UITextField alloc]initWithFrame:CGRectMake(60, 5, width-120, 30)];
    txtname.textColor=[UIColor blackColor];
    txtname.tintColor=backcolor;
    txtname.backgroundColor=[UIColor clearColor];
    txtname.placeholder=@"用户名";
    txtname.font=[UIFont fontWithName:@"Arial" size:14.0];
    [uv5 addSubview:txtname];
    
    
    //公司名称
    txtcompanyname=[[UITextField alloc]initWithFrame:CGRectMake(60, 5, width-120, 30)];
    txtcompanyname.textColor=[UIColor blackColor];
    txtcompanyname.tintColor=backcolor;
    txtcompanyname.backgroundColor=[UIColor clearColor];
    txtcompanyname.placeholder=@"公司名称";
    txtcompanyname.font=[UIFont fontWithName:@"Arial" size:14.0];
    [uv6 addSubview:txtcompanyname];
    
    
    
    
    UIButton *btnOk=[[UIButton alloc]initWithFrame:CGRectMake(40, 385, width-80, 45)];
    [btnOk setTitle:@"注册" forState:UIControlStateNormal];
    [btnOk setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btnOk.font=[UIFont fontWithName:@"Arial" size:14.0];
    btnOk.backgroundColor=[UIColor colorWithRed:194.0/255 green:7.0/255 blue:30.0/255 alpha:1.0f];
    btnOk.layer.cornerRadius=5.0;
    [btnOk addTarget:self action:@selector(registerAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnOk];
    
    UILabel *lbEula=[[UILabel alloc]initWithFrame:CGRectMake(0, 440, width, 20)];
    lbEula.text=@"轻触上面的“注册”按钮，即表示你同意";
    lbEula.textColor=backcolor;
    lbEula.font=[UIFont fontWithName:@"Arial" size:11.0];
    lbEula.textAlignment=UITextAlignmentCenter;
    [self.view addSubview:lbEula];
    
    UIButton *btnEula=[[UIButton alloc]initWithFrame:CGRectMake(0, 460, width, 20)];
    [btnEula setTitle:@"《袜之源软件许可及服务协议》" forState:UIControlStateNormal];
    [btnEula setTitleColor:[UIColor colorWithRed:22.0/255 green:98.0/255 blue:238.0/255 alpha:1.0f] forState:UIControlStateNormal];
    btnEula.font=[UIFont fontWithName:@"Arial" size:11.0];
    [btnEula addTarget:self action:@selector(eulaAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnEula];
    
    
    
    
}

-(void)sendAction:(id)sender{
    
    if([txttel.text isEqualToString:@""]){
        [HTFrameAlert hmAlertInfo:@"请输入手机号"];
        [txttel becomeFirstResponder];
        return;
    }
    
    [self loading];
    
    NSString *serverUrl=[[NSString alloc]initWithFormat:@"%@\/%@\/",[HTSysParam erpServer],[HTSysParam erpVirDir]];
    HTFrameNet *htnet=[[HTFrameNet alloc] initWithServerUrl:serverUrl];
    
    
    NSDictionary *dict =@{@"phoneNumber":txttel.text};
    [htnet hmNetWCFmethodName:@"web/verCode/sendVerCode" andDic:dict complete:^(NSString *backstr)
     {
         NSDictionary *formdict=[HTFrameNet hmConvertDictFromJsonStr:backstr];//表单单头
         int status=[[formdict objectForKey:@"status"]intValue];
         if(status==0){
             [webview removeFromSuperview];
             [HTFrameAlert hmAlertInfo:@"发送成功"];
             [btnsend setUserInteractionEnabled:NO];
             timer =  [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
             score=120;
             
         }
         else{
             [webview removeFromSuperview];
             [HTFrameAlert hmAlertInfo:@"手机号格式不正确"];
         }
         
         
     }];
}


- (IBAction)cityClickAction:(UIButton *)sender {
    UITapGestureRecognizer *sub=(UITapGestureRecognizer *)sender;
    NSString *storyboard=@"LoadCity";
    NSString *Identifier=@"LoadCity";
    UIStoryboard *valueView = [UIStoryboard storyboardWithName:storyboard bundle:nil];
    htvloadcity *uv=[valueView instantiateViewControllerWithIdentifier:Identifier];
    uv.delegate=self;
    UINavigationController *baNavigation = [[UINavigationController alloc] initWithRootViewController:uv];
    
    [self presentModalViewController:baNavigation animated:YES];
    [self dismissModalViewControllerAnimated:YES];
    
}

-(void)cityChanging{
    cityCode=[HTSysParam cityCode];
    cityName=[HTSysParam city];
    [btnSelectCity setTitle:[[NSString alloc]initWithFormat:@"%@ →",cityName] forState:UIControlStateNormal];
}

-(void)timerAction:(id)sender{
    NSString *title=[[NSString alloc]initWithFormat:@"%i",score];
    if(score==1)
    {
        [timer invalidate];
        [btnsend setTitle:@"发送" forState:UIControlStateNormal];
        [btnsend setUserInteractionEnabled:YES];
    }
    else{
        [btnsend setTitle:title forState:UIControlStateNormal];
    }
    score=score-1;
}

-(void)registerAction:(id)sender{
    
    if([txttel.text isEqualToString:@""]){
        [HTFrameAlert hmAlertInfo:@"请输入手机号！"];
        [txttel becomeFirstResponder];
        return;
    }
    
    if([txtno.text isEqualToString:@""]){
        [HTFrameAlert hmAlertInfo:@"请输入验证码！"];
        [txtno becomeFirstResponder];
        return;
    }
    
    if([txtpassword.text isEqualToString:@""]){
        [HTFrameAlert hmAlertInfo:@"请输入密码！"];
        [txtpassword becomeFirstResponder];
        return;
    }
    
    if([txtpassword2.text isEqualToString:@""]){
        [HTFrameAlert hmAlertInfo:@"请输入确认密码！"];
        [txtpassword2 becomeFirstResponder];
        return;
    }
    
    if(![txtpassword.text isEqualToString:txtpassword2.text]){
        [HTFrameAlert hmAlertInfo:@"两次输入的密码不一致，请重新输入！"];
        [txtpassword2 becomeFirstResponder];
        return;
    }
    
    if([txtname.text isEqualToString:@""]){
        [HTFrameAlert hmAlertInfo:@"请输入用户名！"];
        [txtname becomeFirstResponder];
        return;
    }
    
    if([txtcompanyname.text isEqualToString:@""]){
        [HTFrameAlert hmAlertInfo:@"请输入公司名称！"];
        [txtcompanyname becomeFirstResponder];
        return;
    }
    
    [self loading];
    NSString *serverUrl=[[NSString alloc]initWithFormat:@"%@\/%@\/",[HTSysParam erpServer],[HTSysParam erpVirDir]];
    HTFrameNet *htnet=[[HTFrameNet alloc] initWithServerUrl:serverUrl];
    
    
    NSDictionary *dict =@{@"mobile":txttel.text,@"name":txtname.text,@"password":txtpassword.text,@"verCode":txtno.text,@"companyName":txtcompanyname.text};
    [htnet hmNetWCFmethodName:@"web/member/regist" andDic:dict complete:^(NSString *backstr)
     {
         NSDictionary *formdict=[HTFrameNet hmConvertDictFromJsonStr:backstr];//表单单头
         int status=[[formdict objectForKey:@"status"]intValue];
         if(status==0){
             [webview removeFromSuperview];
             [HTFrameAlert hmAlertInfo:@"注册成功"];
             [HTSysParam loadParamWithTelNum:txttel.text];
             [self dismissViewControllerAnimated: YES completion: nil ];
             
         }
         else{
             NSDictionary *error=[formdict objectForKey:@"error"];
             NSString *code=[error objectForKey:@"code"];
             if([code isEqualToString:@"110102"]){
                 [HTFrameAlert hmAlertInfo:@"用户已注册"];
             }
             if([code isEqualToString:@"130001"]){
                 [HTFrameAlert hmAlertInfo:@"验证码无效"];
             }
             if([code isEqualToString:@"130002"]){
                 [HTFrameAlert hmAlertInfo:@"验证码错误"];
             }

             [webview removeFromSuperview];
             
         }
         
         
     }];
    
}

-(void)moveKeyAction:(UITapGestureRecognizer *)sender {
    [txttel resignFirstResponder];
    [txtno resignFirstResponder];
    [txtpassword resignFirstResponder];
    [txtpassword2 resignFirstResponder];
    [txtname resignFirstResponder];
    [txtcompanyname resignFirstResponder];
}


-(void)eulaAction:(UIButton*)sender{
    
    UITapGestureRecognizer *sub=(UITapGestureRecognizer *)sender;
    NSString *storyboard=@"Eula";
    NSString *Identifier=@"Eula";
    UIStoryboard *valueView = [UIStoryboard storyboardWithName:storyboard bundle:nil];
    UIViewController *uv=[valueView instantiateViewControllerWithIdentifier:Identifier];
    UINavigationController *baNavigation = [[UINavigationController alloc] initWithRootViewController:uv];
    [self presentModalViewController:baNavigation animated:YES];
    
    //返回

    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}


- (IBAction)backAction:(UIButton *)sender {
     [self dismissViewControllerAnimated: YES completion: nil ];
}
@end
